package com.zzjc.web.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class DateUtil
{
    private static String DEFAULT_DATETIME_FORMAT = "yyyy年MM月dd日";
    private static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static String DEFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static String DEFAULT_TIME_FORMAT_STRING = "yyyyMMddHHmmss";
    private static SimpleDateFormat DEFAULT_DATETIME_FORMATTER = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
    private static SimpleDateFormat DEFAULT_DATE_FORMATTER = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
    private static SimpleDateFormat DEFAULT_TIME_FORMATTER = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
    private static SimpleDateFormat DEFAULT_TIME_FORMATTER_STRING = new SimpleDateFormat(DEFAULT_TIME_FORMAT_STRING);

    public static String formatDate(Date d)
    {
        return DEFAULT_DATE_FORMATTER.format(d);
    }

    public static String formatDate(long d)
    {
        return DEFAULT_DATE_FORMATTER.format(Long.valueOf(d));
    }

    public static String formatDate(Date d, String fmt)
    {
        SimpleDateFormat simpleDateFormat = fmt == null ? DEFAULT_TIME_FORMATTER : new SimpleDateFormat(fmt);
        return simpleDateFormat.format(d == null ? new Date() : d);
    }

    public static String formatDate(long d, String fmt)
    {
        SimpleDateFormat simpleDateFormat = fmt == null ? DEFAULT_TIME_FORMATTER : new SimpleDateFormat(fmt);
        return simpleDateFormat.format(Long.valueOf(d));
    }

    public static String formatDateTime(Date d)
    {
        return DEFAULT_TIME_FORMATTER.format(d);
    }

    public static String formatDateTime(long d)
    {
        return DEFAULT_TIME_FORMATTER.format(Long.valueOf(d));
    }

    public static String formatDateTimeString(Date d)
    {
        return DEFAULT_TIME_FORMATTER_STRING.format(d);
    }

    public static Date getDate(String str)
    {
        try
        {
            return DEFAULT_DATE_FORMATTER.parse(str);
        }
        catch (ParseException e)
        {
        }
        return null;
    }

    public static Date getTime(String str)
    {
        try
        {
            return DEFAULT_TIME_FORMATTER.parse(str);
        }
        catch (ParseException e)
        {
        }
        return null;
    }

    public static Date getDate(String str, String parse)
    {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(parse);
        try
        {
            return simpleDateFormat.parse(str);
        }
        catch (ParseException e)
        {
        }
        return null;
    }

    public static String getTodayDate(String dateFormat)
    {
        if (StringKit.notBlank(dateFormat)) {
            return new SimpleDateFormat(dateFormat).format(new Date());
        }
        return getStringDate();
    }

    public static Date getTodayDateNoSFM()
    {
        try
        {
            return DEFAULT_DATE_FORMATTER.parse(DEFAULT_DATE_FORMATTER.format(new Date()));
        }
        catch (ParseException e) {}
        return null;
    }

    public static String getStringDate()
    {
        return DEFAULT_TIME_FORMATTER.format(new Date());
    }

    public static String getStringDateShort()
    {
        return DEFAULT_DATE_FORMATTER.format(new Date());
    }

    public static Date getLastDate(long day)
    {
        Date date = new Date();
        long date_3_hm = date.getTime() - 86400000L * day;
        Date date_3_hm_date = new Date(date_3_hm);
        return date_3_hm_date;
    }

    public static String getLastDate(int yearNum, String time)
    {
        Date date = getDate(time);
        long days = yearNum * 365;
        long date_3_hm = date.getTime() - 86400000L * days;
        Date date_3_hm_date = new Date(date_3_hm);
        return DEFAULT_DATE_FORMATTER.format(date_3_hm_date);
    }

    public static boolean isLeapYear(String ddate)
    {
        Date d = getDate(ddate);
        GregorianCalendar gc = (GregorianCalendar)Calendar.getInstance();
        gc.setTime(d);
        int year = gc.get(1);
        if (year % 400 == 0) {
            return true;
        }
        if (year % 4 == 0)
        {
            if (year % 100 == 0) {
                return false;
            }
            return true;
        }
        return false;
    }

    public static String getSeqWeek()
    {
        Calendar c = Calendar.getInstance(Locale.CHINA);
        String week = Integer.toString(c.get(3));
        if (week.length() == 1) {
            week = "0" + week;
        }
        return week;
    }

    public static String getSeqWeek(String date)
    {
        Date d = getDate(date);
        GregorianCalendar gc = (GregorianCalendar)Calendar.getInstance();
        gc.setTime(d);
        String week = Integer.toString(gc.get(3));
        if (week.length() == 1) {
            week = "0" + week;
        }
        return week;
    }

    public static String getWillDate2(String format, int month)
    {
        String tmpret = "";

        Calendar ca = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        ca.add(2, month);
        tmpret = sdf.format(ca.getTime());
        return tmpret;
    }

    public static String getWillDate2(String format, String data1, int month)
            throws ParseException
    {
        SimpleDateFormat f = new SimpleDateFormat(format);
        Date date1 = f.parse(data1);
        Calendar c = Calendar.getInstance();
        c.setTime(date1);
        c.add(2, month);
        Date date2 = c.getTime();
        return f.format(date2);
    }

    public static String getbeforeDateByYear(int yearNum)
    {
        long days = yearNum * 365;
        return DEFAULT_DATE_FORMATTER.format(getLastDate(days));
    }

    public static String formatLateDate(int day)
    {
        Calendar cal = Calendar.getInstance();
        cal.add(5, day);
        return DEFAULT_DATE_FORMATTER.format(cal.getTime());
    }

    public static Timestamp getTimestamp(Timestamp data1, int month)
            throws ParseException
    {
        Calendar c = Calendar.getInstance();
        c.setTime(data1);
        c.add(2, month);


        return new Timestamp(c.getTimeInMillis());
    }

    public static int differenceMonth(String data1, String data2)
    {
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        int result = 0;
        int month = 0;
        try
        {
            bef.setTime(DEFAULT_DATE_FORMATTER.parse(data1));
            aft.setTime(DEFAULT_DATE_FORMATTER.parse(data2));
            result = aft.get(2) - bef.get(2);
            month = (aft.get(1) - bef.get(1)) * 12;
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
        return Math.abs(month + result);
    }

    public static int getMonthSpace(String date1, String date2)
            throws ParseException
    {
        int result = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(sdf.parse(date1));
        c2.setTime(sdf.parse(date2));
        int y1 = c1.get(1);int y2 = c2.get(1);
        int m1 = c1.get(2);int m2 = c2.get(2);
        int d1 = c1.get(5);int d2 = c2.get(5);
        result = (y1 - y2) * 12 + (m1 - m2);
        if (d1 >= d2) {
            result++;
        }
        return result == 0 ? 1 : Math.abs(result);
    }

    public static String formatDateDay(Date d)
    {
        return new SimpleDateFormat("yyyy-MM").format(d);
    }

    public static Date getDateDay(String str)
    {
        try
        {
            return new SimpleDateFormat("yyyy-MM").parse(str);
        }
        catch (ParseException e)
        {
        }
        return null;
    }

    public static Date getDateSTRING(String str)
    {
        try
        {
            return DEFAULT_TIME_FORMATTER.parse(str);
        }
        catch (ParseException e)
        {
        }
        return null;
    }

    public static String getYearMonthDay(String yearMonth)
    {
        String returnStr = "";
        String monthDay = "";
        if (StringKit.notBlank(yearMonth))
        {
            int y = 0;
            int n = 0;
            String[] strArr = yearMonth.split("-");
            y = Integer.parseInt(strArr[0]);
            n = Integer.parseInt(strArr[1]);
            switch (n)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    monthDay = "31";
                    break;
                case 2:
                    if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {
                        monthDay = "29";
                    } else {
                        monthDay = "28";
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    monthDay = "30";
                    break;
                default:
                    monthDay = "31";
            }
            returnStr = yearMonth + "-" + monthDay;
        }
        return returnStr;
    }

    public static String getLastMonthDay(String d)
    {
        Date date = getDate(d);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(2, -1);
        cal.set(5, cal.getActualMaximum(5));
        Date lastDateOfPrevMonth = cal.getTime();
        System.out.println();
        return formatDate(lastDateOfPrevMonth);
    }

    public static String getYearLast(int year)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(1, year);
        calendar.roll(6, -1);
        Date currYearLast = calendar.getTime();

        return formatDate(currYearLast);
    }

    public static Integer getDayTime(Date date1, Date date2)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        String dbtime1 = format.format(date1);

        String dbtime2 = format.format(date2);

        Date date1Tmp = null;
        Date date2Tmp = null;
        try
        {
            date1Tmp = format.parse(dbtime1);
            date2Tmp = format.parse(dbtime2);
        }
        catch (ParseException e)
        {
        }
        Integer daynum = Integer.valueOf((int)((date1Tmp.getTime() - date2Tmp.getTime()) / 86400000L));
        return daynum;
    }

    public static String getWillDate(String data1, int month)
            throws ParseException
    {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = f.parse(data1);
        Calendar c = Calendar.getInstance();
        c.setTime(date1);
        c.add(2, month);
        c.add(6, -2);
        Date date2 = c.getTime();
        return f.format(date2);
    }

    public static String formatDateTimeFormat(Date d)
    {
        return DEFAULT_DATETIME_FORMATTER.format(d);
    }

    public static boolean getDateDiffer(Date date1, Date data2, double cha)
    {
        boolean diff = false;
        if (date1.getTime() - data2.getTime() >= cha * 365.0D * 24.0D * 60.0D * 60.0D * 1000.0D) {
            diff = true;
        }
        return diff;
    }

    public static Date replaceYear(String date, String parse)
    {
        if (StringKit.notBlank(date)) {
            try
            {
                return new SimpleDateFormat(parse).parse(date.replace("年", "-").replace("月", "-").replace("日", ""));
            }
            catch (ParseException e)
            {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    public static boolean getDateDifferSmall(Date date1, Date data2, double cha)
    {
        boolean diff = false;
        if (date1.getTime() - data2.getTime() <= cha * 365.0D * 24.0D * 60.0D * 60.0D * 1000.0D) {
            diff = true;
        }
        return diff;
    }

    public static boolean comparetoTime(String beginTime, String endTime)
            throws ParseException
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date bt = sdf.parse(beginTime);
        Date et = sdf.parse(endTime);
        if (bt.getTime() > et.getTime()) {
            return true;
        }
        return false;
    }

    /**
     * 给当前日期加、减天数
     * @param format 格式:例如"yyyy-MM-dd HH:mm:ss"
     * @param days 天数
     * @return
     */
    public static String addDate(String format, String days){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, Integer.parseInt(days));
        Date d = c.getTime();
        String day = sdf.format(d);
        return day;
    }
}
